package com.wanqing.community.controller;


import com.wanqing.community.bean.GoodsBean;
import com.wanqing.community.controller.util.BaseController;
import com.wanqing.community.manager.GoodsManager;
import com.wanqing.community.util.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;

/**
 * 商品表展示层.
 */
@Controller
@RequestMapping("goodsController")
public class GoodsController extends BaseController {

    /** The log. */
    private static Logger log = Logger.getLogger(GoodsController.class);// 日志

    /** The t goods manager. */
    @Resource
    private GoodsManager goodsManager;

    /**
     * 进入list界面.
     * @param request the request
     * @param response the response
     * @return the all t goods
     */
    @RequestMapping(params = "method=list")
    public ModelAndView getAllTGoods(HttpServletRequest request, HttpServletResponse response) {
        clearCache(response);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("biz/goods/query/list");//进入页面
        return mav;
    }

    /**
     * 判断type类型.
     * @param request the request
     * @param response the response
     * @return the model and view
     */
    @RequestMapping(params = "method=index")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        String type = StringTools.objectToString(request.getParameter("type"));
        mav.addObject("currentDateTime",
                (new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new java.util.Date()).toString());
        mav.addObject("currentDate", (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(new java.util.Date())
                .toString());
        if ("list".equals(type)) {
            mav.setViewName("biz/goods/query/list");
        } else if ("add".equalsIgnoreCase(type)) {
            mav.setViewName("biz/goods/create/add");
        } else if ("update".equals(type)) {
            String gsId = StringTools.objectToString(request.getParameter("iaId"));//先拿到选中这一条的id
            GoodsBean good = this.goodsManager.findGoodsById(gsId);//根据id查询这一个商品的信息
            mav.addObject("good", good);//把这个商品的信息添加到页面
            mav.setViewName("biz/goods/create/modi");//跳转到修改页面
        }
        return mav;
    }
    /**
     * 获得list数据.
     * @param request the request
     * @param response the response
     */
    @RequestMapping(params = "method=listData")
    public void getAllTGoodsData(@Valid @ModelAttribute GoodsBean goodsBean, HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String,Object> mapPram = new HashMap<>();
            mapPram.put("goods",goodsBean);//拿到GoodsBean后塞到map中
            int pageNo = NumberDealTools.stringTranceInt(request.getParameter("page"), 1);
            int pageSize = NumberDealTools.stringTranceInt(request.getParameter("rows"), KeyConstant.PAGESIZE);
            mapPram.put("begin", (pageNo - 1) * pageSize);
            mapPram.put("pageSize", pageSize);
            int total = this.goodsManager.getGoodsCount(mapPram);//查询商品总数
            List<GoodsBean> list = this.goodsManager.getGoods(mapPram);//根据页数查询商品
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("total", total);
            map.put("rows", list);
            String json = StringTools.buildJson(map);
            writeJson(response, json);
        } catch (Exception e) {
            log.error("查询商品列表异常：" + e.getMessage());
        }
    }

    /**
     * 添加产品.
     * @param request the request
     * @param response the response
     * @return the string
     */
    @RequestMapping(params = "method=addGood")
    public String addGood(@Valid @ModelAttribute GoodsBean goodsBean,HttpServletRequest request, HttpServletResponse response) {
        try {
            boolean b = this.goodsManager.addGood(goodsBean);
            if (b) {
                writeText(response, KeyConstant.SUCCESS);
            } else {
                writeJson(response, KeyConstant.FAIL);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            writeJson(response, e.toString());
        }
        return null;
    }

    /**
     * 修改产品.
     * @param request the request
     * @param response the response
     * @return the string
     */
    @RequestMapping(params = "method=updateGood")
    public String updateTGoods(@Valid @ModelAttribute GoodsBean goodsBean,HttpServletRequest request, HttpServletResponse response) {
        try {
            boolean b = this.goodsManager.updateGood(goodsBean);

            if (b) {
                writeText(response, KeyConstant.SUCCESS);
            } else {
                writeText(response, KeyConstant.FAIL);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            writeText(response, e.toString());
        }
        return "";
    }

    /**
     * 删除商品.
     * @param request the request
     * @param response the response
     * @return the string
     */
    @RequestMapping(params = "method=deleteGoods")
    public String deleteGoods(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] roleIds = request.getParameterValues("id[]");
            if (null == roleIds || roleIds.length == 0) {
                writeText(response, KeyConstant.EMPTY);
            } else {
                List<String> list = Arrays.asList(roleIds);
                boolean b = this.goodsManager.deleteGoods(list);
                if (b) {
                    writeText(response, KeyConstant.SUCCESS);
                } else {
                    writeText(response, KeyConstant.FAIL);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            writeText(response, e.toString());
        }
        return null;
    }


}
